from app01 import models
from django import forms
from app01.utils.bootstrap import BootStrapModelForm,BootStrapForm
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
from app01.utils.md5file import  md5
class UserModelForm(BootStrapModelForm):
    name=forms.CharField(min_length=2,label='姓名')
    class Meta:
        model=models.UserInfo
        fields=["name","password","age","account","create_time","gender","depart"]

class PerttyModelForm(BootStrapModelForm):
    #第一法方
    #验证手机格式需要正则表达式
    mobile=forms.CharField(label='手机号',validators=[RegexValidator(r'^1[3-9]\d{9}$','手机格式错误')])

    #第二个方法钩子方法验证
    # def clean_mobile(self):
    #     txt_mobile=self.cleaned_data['mobile']
    #     if len(txt_mobile) != 11:
    #         raise ValidationError('手机格式错误')
    #     return txt_mobile
    # 添加过程中手机号不能重复
    def clean_mobile(self):
        text_mobile=self.cleaned_data['mobile']
        #  判断新增手机号是否重复，还有编辑手机号排除自己
        #  编辑允许自己ID的手机号可以重复
        if models.Pretty.objects.exclude(id=self.instance.pk).filter(mobile=text_mobile).exists():
            raise ValidationError('手机号已存在')
        return text_mobile
    class Meta:
        model=models.Pretty
        fields='__all__'

class AdminModelForm(BootStrapModelForm):
    confirm_password=forms.CharField(label='确认密码',widget=forms.PasswordInput(render_value=True))
    class Meta:
        model=models.Admin
        fields='__all__'
        widgets={'password':forms.PasswordInput(render_value=True),
                 }
    #钩子方法验证密码
    # def clean_password(self):
    #     txt_password=self.cleaned_data['password']
    #     confirm_password=self.cleaned_data['confirm_password']
    #     if  txt_password != confirm_password:
    #         raise ValidationError("密码不一致请重新输入")
    #         return confirm_password
    #     if len(txt_password)<6:
    #         raise ValidationError('密码不能少于6位')
    #     return txt_password

    # MD5加密
    def clean_password(self):
        pwd = self.cleaned_data.get('password')
        return md5(pwd)

    #验证确认密码
    def clean_confirm_password(self):
        txt_password=self.cleaned_data['password']
        confirm_password=md5(self.cleaned_data['confirm_password'])
        if  txt_password != confirm_password:
            raise ValidationError("密码不一致请重新输入")
            return confirm_password
    def clean_name(self):
        text_username=self.cleaned_data['name']
        if models.Admin.objects.exclude(id=self.instance.pk).filter(name=text_username).exists():
            raise ValidationError('用户名已存在')
        return text_username

class LoginModelForm(BootStrapModelForm):
    code = forms.CharField(
        label='验证码',
        widget=forms.TextInput,
        required=True
    )
    class Meta:
        model=models.Admin
        fields=['name','password','code']
        widgets={'password':forms.PasswordInput(render_value=True)}
    def clean_password(self):
        pwd=self.cleaned_data.get("password")
        return md5(pwd)

class TaskModelForm(BootStrapModelForm):
    class Meta:
        model=models.Task
        fields="__all__"
        widgets={'level':forms.Select(choices=((1,'优先级1'),(2,'优先级2'),(3,'优先级3'))),'detial':forms.TextInput}

class OrderModelForm(BootStrapModelForm):
    class Meta:
        model=models.Order
        exclude=['admin','id']

class upForm(BootStrapForm):
    bootStrap_exclude_field = ["img"]
    name = forms.CharField(label='姓名',max_length=32)
    age = forms.IntegerField(label='年龄')
    img = forms.FileField(label='头像')

class upModelForm(BootStrapModelForm):
    bootStrap_exclude_field=['img']
    class Meta:
        model=models.City
        fields="__all__"

class departForm(BootStrapModelForm):
    class Meta:
        model=models.Department
        fields="__all__"

class cityForm(BootStrapModelForm):
    bootStrap_exclude_field = ['img']
    class Meta:
        model=models.City
        fields="__all__"




